package com.zjzaki.thingsboard.ui.main;

import androidx.arch.core.util.Function;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations;
import androidx.lifecycle.ViewModel;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zjzaki.thingsboard.dao.ThingsBoardDao;

import java.util.LinkedHashMap;
import java.util.Map;

public class PageViewModel extends ViewModel {

    /**
     * 设备id
     */
    private String deviceId;

    /**
     * 设备名
     */
    private String deviceName;

    /**
     * token值
     */
    private String token;

    private MutableLiveData<Integer> mIndex = new MutableLiveData<>();
    private LiveData<String> mText = Transformations.map(mIndex, new Function<Integer, String>() {
        @Override
        public String apply(Integer input) {
            return "Hello world from section: " + input;
        }
    });

    private LiveData<Map<Long, Float>> mData = Transformations.map(mIndex, new Function<Integer, Map<Long, Float>>() {
        @Override
        public Map<Long, Float> apply(Integer input) {
            System.out.println(input);
            //定义一个列表
            Map<Long, Float> listData = new LinkedHashMap<>();
            //获取当前时间的时间戳
            long currentTimestamps = System.currentTimeMillis();
            //计算一天的时间戳
            long oneDayTimestamps = Long.valueOf(60 * 60 * 24 * 1000);
            //计算今天零时的时间戳
            long todayTimestamps = currentTimestamps - (currentTimestamps + 60 * 60 * 8 * 1000) % oneDayTimestamps;
            System.out.println(todayTimestamps);
            //判断点击的是第几个tab页
            switch (input) {
                case 1:
                    //查询今天的历史数据
                    String historicalData = ThingsBoardDao.getHistoricalData(deviceId, "temp",
                            todayTimestamps, todayTimestamps + 60 * 60 * 24 * 1000, 0
                            , 500, "AVG", token);
                    System.out.println(historicalData);
                    //将字符串装换为JSON对象
                    JSONObject jsonObject = JSONObject.parseObject(historicalData);
                    //获取JSONArray对象
                    JSONArray temp = jsonObject.getJSONArray("temp");
                    //判断是否为空值
                    if (temp != null) {
                        for (int i = temp.size(); i > 0; i--) {
                            JSONObject tempJson = (JSONObject) temp.get(i - 1);
                            listData.put(tempJson.getLong("ts"), tempJson.getFloat("value"));
                        }
                    }

                    break;
                case 2:
                    //查询昨天的历史数据
                    String historicalDataByYesterday = ThingsBoardDao.getHistoricalData(deviceId, "temp",
                            todayTimestamps - 60 * 60 * 24 * 1000, todayTimestamps, 0
                            , 500, "AVG", token);
                    System.out.println(historicalDataByYesterday);
                    //将字符串装换为JSON对象
                    JSONObject jsonObjectYesterday = JSONObject.parseObject(historicalDataByYesterday);
                    //获取JSONArray对象
                    JSONArray tempYesterday = jsonObjectYesterday.getJSONArray("temp");
                    //判断是否为空值
                    if (tempYesterday != null) {
                        for (int i = tempYesterday.size(); i > 0; i--) {
                            JSONObject tempJson = (JSONObject) tempYesterday.get(i - 1);
                            listData.put(tempJson.getLong("ts"), tempJson.getFloat("value"));
                        }
                    }
                    break;
                case 3:
                    //查询近七天的历史数据
                    String historicalDataLastSevenDays = ThingsBoardDao.getHistoricalData(deviceId, "temp",
                            todayTimestamps - oneDayTimestamps * 7, todayTimestamps, 0
                            , 500, "AVG", token);
                    //将字符串装换为JSON对象
                    JSONObject jsonObjectLastSevenDays = JSONObject.parseObject(historicalDataLastSevenDays);
                    //获取JSONArray对象
                    JSONArray tempLastSevenDays = jsonObjectLastSevenDays.getJSONArray("temp");
                    //判断是否为空值
                    if (tempLastSevenDays != null) {
                        for (int i = tempLastSevenDays.size(); i > 0; i--) {
                            JSONObject tempJson = (JSONObject) tempLastSevenDays.get(i - 1);
                            listData.put(tempJson.getLong("ts"), tempJson.getFloat("value"));
                        }
                    }
                    break;
            }

            //返回数据
            return listData;
        }
    });

    public LiveData<Map<Long, Float>> getData() {
        return mData;
    }

    public void setIndex(int index) {
        mIndex.setValue(index);
    }

    /**
     * 设置信息
     *
     * @param index      tab页索引
     * @param deviceId   设备id
     * @param deviceName 设备名
     */
    public void setInfo(int index, String deviceId, String deviceName, String token) {
        mIndex.setValue(index);
        this.deviceId = deviceId;
        this.deviceName = deviceName;
        this.token = token;
    }

    public LiveData<String> getText() {
        return mText;
    }
}